package com.server.http.handler.impl;

import java.io.IOException;

import com.server.conditionenum.HttpReturnStatus;
import com.server.db.DataBaseManager;
import com.server.http.handler.HttpHandler;
import com.server.http.request.HttpRequestMessage;
import com.server.http.response.HttpResponseMessage;
import com.server.mine.net.HttpProtocol;
import com.server.tools.AppData;
import com.server.tools.Tools;

/**
* @author	李小布
* @email	397554852@qq.com
* @version	创建时间：2018年9月27日 下午6:26:11
*/
public class GetData extends HttpHandler{
	// -1上传参数错误;0;-2APP不存在;-3数据库异常;-4未知错误;-5解密失败;
	@Override
	public void handle(HttpRequestMessage request, HttpResponseMessage response) {
		int failMsg = 0;
		long uid = 0;
		long appid = 0;
		String key = request.getParameter("key");
		String data = null;
		if(key == null || "".equals(key)) {
			failMsg = -1;
		}else {
			try {
				uid =Long.parseLong(request.getParameter("uid"));
				appid = Long.parseLong(request.getParameter("appid"));
				if(!AppData.appMap.containsKey(appid)) failMsg = -2;
			} catch (Exception e) {
				failMsg = -5;
			}
		}
		if(failMsg == 0) {
			String value = DataBaseManager.GetInstance().GetData(uid, appid, key);
			if(value != null) {
				try {
					data = Tools.uncompress(value, AppData.appMap.get(appid).appkey);
				} catch (IOException e) {
					failMsg = -5;
				}
			}else {
				failMsg = 0;
				data=null;
			}
		}
		
		if(failMsg == 0 && data != null) {
			SetReturnData(response,data,HttpProtocol.GETDATA,HttpReturnStatus.Successed);
		}else {
			SetReturnData(response,failMsg,HttpProtocol.GETDATA,HttpReturnStatus.Failed);
		}
		
	}

}